package com.jy.mybatis;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
 
/**
 * 生成数据库字段注释
 * 类名前加自定义注释
 * 注释增加数据字段是否为必填项
 * 注释增加数据字段是否有默认值
 * 注释增加属性是否为主健
 * @version:1.0
 * @Description: RemarksCommentGenerator
 * @author: jiangyan 
 * @date: 2018年9月20日
 */
public class RemarksCommentGenerator extends DefaultCommentGenerator {
 
    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * @fileName "+introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(" * @date "+new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        topLevelClass.addJavaDocLine(" * @author:jiangyan ");
        topLevelClass.addJavaDocLine(" */");
        topLevelClass.addAnnotation("@Table(name=\"" + introspectedTable.getFullyQualifiedTable() + "\")");
        topLevelClass.addImportedType("javax.persistence.Table");
        topLevelClass.addImportedType("javax.persistence.Column");
    }
    
    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        String remark = introspectedColumn.getRemarks();
        field.addJavaDocLine("/** ");
        field.addJavaDocLine(" * " + remark);
        field.addJavaDocLine(" */");
        String serializeId = "serialVersionUID";
        if(!serializeId.equals(field.getName())) {
        	String columnName = introspectedColumn.getActualColumnName();
        	field.addAnnotation("@Column(name=\"" + columnName + "\")");
        }
    }
    
    @Override
    public void addGetterComment(Method method, IntrospectedTable introspectedTable,
    		IntrospectedColumn introspectedColumn) {
    	method.addJavaDocLine("/** ");
    	method.addJavaDocLine(" * " + method.getName() + "(get方法)");
    	method.addJavaDocLine(" */");
    }
    
    @Override
    public void addSetterComment(Method method, IntrospectedTable introspectedTable,
    		IntrospectedColumn introspectedColumn) {
    	method.addJavaDocLine("/** ");
    	method.addJavaDocLine(" * " + method.getName() + "(set方法)");
    	method.addJavaDocLine(" */");
    }
    
}
